package io.sage.modules.sys.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import io.sage.modules.sys.entity.SysUserEntity;
import io.sage.modules.sys.shiro.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import io.sage.common.annotation.DataFilter;
import io.sage.common.utils.Constant;
import io.sage.common.utils.RedisUtils;
import io.sage.modules.sys.dao.SysDeptDao;
import io.sage.modules.sys.entity.SysDeptEntity;
import io.sage.modules.sys.service.SysDeptService;


@Service("sysDeptService")
public class SysDeptServiceImpl extends ServiceImpl<SysDeptDao, SysDeptEntity> implements SysDeptService {
	
	@Autowired
	private SysDeptDao sysDeptDao;
	
	@Autowired
	private RedisUtils redisUtils;
	
	@Override
	@DataFilter(subDept = false, user = false)
	public List<SysDeptEntity> queryList(Map<String, Object> params){
		List<SysDeptEntity> deptList =
			this.selectList(new EntityWrapper<SysDeptEntity>()
			.addFilterIfNeed(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER)));

		for(SysDeptEntity sysDeptEntity : deptList){
			SysDeptEntity parentDeptEntity =  this.selectById(sysDeptEntity.getParentId());
			if(parentDeptEntity != null){
				sysDeptEntity.setParentName(parentDeptEntity.getName());
			}
		}
		return deptList;
	}

	@Override
	public List<Long> queryDetpIdList(Long parentId) {
		return baseMapper.queryDetpIdList(parentId);
	}

	@Override
	public List<Long> getSubDeptIdList(Long deptId){
		//部门及子部门ID列表
		List<Long> deptIdList = new ArrayList<>();

		//获取子部门ID
		List<Long> subIdList = queryDetpIdList(deptId);
		getDeptTreeList(subIdList, deptIdList);

		return deptIdList;
	}

	/**
	 * 递归
	 */
	private void getDeptTreeList(List<Long> subIdList, List<Long> deptIdList){
		for(Long deptId : subIdList){
			List<Long> list = queryDetpIdList(deptId);
			if(list.size() > 0){
				getDeptTreeList(list, deptIdList);
			}

			deptIdList.add(deptId);
		}
	}

	@Override
	public List<SysDeptEntity> getSerialNo(String deptNo) {
		
		return baseMapper.getDeptNo(deptNo);
	}
	
	 /**
     * 根据部门的编号及编号长度查询数据.
     * @param deptNo
     * @param deptLength
     * @return
     */
	@Override
	public List<SysDeptEntity> getDeptList(String deptNo, Integer deptLength) {
		return baseMapper.getDeptList(deptNo, deptLength);
	}

	@Override
	@DataFilter(tableAlias="d",subDept = false, user = false)
	public List<SysDeptEntity> queryList1(Map<String, Object> params) {
		
		List<SysDeptEntity> selectDeptList ;
//		Object string = redisUtils.get("deptList",List.class);
//		if("null".equals(string)||string==null){
			selectDeptList = sysDeptDao.selectDeptList(params);
//			redisUtils.set("deptList", selectDeptList);
//		}else {
			
//			selectDeptList=(List<SysDeptEntity>) string;
//		}
		
		return selectDeptList;
	}

	@Override
	public SysDeptEntity getDeptById(Long deptId) {

		return sysDeptDao.getDeptById(deptId);
	}
	@Override
	public SysDeptEntity getDeptByNo(String deptNo) {

		return sysDeptDao.getDeptByNo(deptNo);
	}

	@Override
	public List<SysDeptEntity> queryListByUser(Map map) {
		return sysDeptDao.queryListByUser((Long)map.get("deptId"));
	}

	@Override
	public List<Long> getParenTListByDeptId(Long deptId) {
		List<Long>list=new ArrayList<>();
		List<SysDeptEntity> deptList = this.sysDeptDao.selectDeptList(null);
		Collections.sort(deptList);
		Map<Long, List<SysDeptEntity>> mapList = deptList.parallelStream().collect(Collectors.groupingBy(SysDeptEntity::getDeptId));
		SysUserEntity user = ShiroUtils.getUserEntity();
		SysDeptEntity sysDeptEntity=mapList.get(deptId).get(0);
		list.add(sysDeptEntity.getDeptId());
		boolean flag=true;
		while (flag){
			if(mapList.get(sysDeptEntity.getParentId())==null){
				flag=false;
				break;
			}
			sysDeptEntity=mapList.get(sysDeptEntity.getParentId()).get(0);
			list.add(sysDeptEntity.getDeptId());
			if(sysDeptEntity.getDeptId()==user.getDeptId()){
				flag=false;
				break;
			}
		}

		return list;
	}
}
